document.addEventListener("DOMContentLoaded", function(event) {
   
    const showNavbar = (toggleId, navId, bodyId, headerId) =>{
        const toggle = document.getElementById(toggleId),
        nav = document.getElementById(navId),
        bodypd = document.getElementById(bodyId),
        headerpd = document.getElementById(headerId)
        
        // 验证所有变量是否存在
        if(toggle && nav && bodypd && headerpd){
            toggle.addEventListener('click', ()=>{
                // 显示导航栏
                nav.classList.toggle('show')
                // 更改图标
                toggle.classList.toggle('bx-x')
                // 添加内边距到body
                bodypd.classList.toggle('body-pd')
                // 添加内边距到header
                headerpd.classList.toggle('body-pd')
            })
        }
    }
    
    showNavbar('header-toggle','nav-bar','body-pd','header')
    
    /*===== 链接激活 =====*/
    const linkColor = document.querySelectorAll('.nav_link')
    
    function colorLink(){
        if(linkColor){
            linkColor.forEach(l=> l.classList.remove('active'))
            this.classList.add('active')
        }
    }
    linkColor.forEach(l=> l.addEventListener('click', colorLink))
    
    // 根据当前URL激活对应的导航链接
    const currentPath = window.location.pathname;
    linkColor.forEach(link => {
        const href = link.getAttribute('href');
        if (href && currentPath.includes(href)) {
            link.classList.add('active');
        }
    });
}); 